package handler

import (
	"github.com/gin-gonic/gin"
	"proj-dev-sys/model"
	"strconv"
)

func Good(c *gin.Context) {
	title := c.PostForm("title")
	name := c.PostForm("name")
	price := c.PostForm("price")
	if title == "" || name == "" || price == "" {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "字段不能为空",
		})
		return
	}
	data := model.Good{

		Title: title,
		Name:  name,
		Price: price,
	}
	err := model.DB.Create(&data).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 400,
			"msg":  "添加失败",
		})
		return
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func List(c *gin.Context) {

	page, _ := strconv.Atoi(c.Query("page"))
	size, _ := strconv.Atoi(c.Query("size"))

	offset := (page - 1) * size

	var list []model.Good
	model.DB.Offset(offset).Limit(size).Find(&list)

	var todle int64
	model.DB.Model(model.Good{}).Count(&todle)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"todle": todle,
	})
	return
}
